package com.ch.demo;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

public class Demo2 {
    public static void main(String[] args) {

    }
    public static void check(Object t) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class<?> aClass = t.getClass();
        System.out.println("aClass.getName() = " + aClass.getName());
        System.out.println("aClass.getSimpleName() = " + aClass.getSimpleName());

        System.out.println("公共属性 = " + Arrays.toString(aClass.getFields()));
        System.out.println("定义的属性 = " + Arrays.toString(aClass.getDeclaredFields()));

        Field name = aClass.getField("name");
        name.set(t,"张三");
        Object o = name.get(t);
        System.out.println("o = " + o);
        Field age = aClass.getDeclaredField("age");
        age.setAccessible(true);
        age.set(t,10);
        System.out.println("age.get(t) = " + age.get(t));

        Method eat = aClass.getDeclaredMethod("eat", String.class);
        eat.setAccessible(true);
        eat.invoke(t,"肯德基");

        aClass.getMethod("eat",String.class);

    }
}
class B extends A{
    String type;
    void sleep(){

    }
}
class A{
    private int age;
    public String name;

    public void play(){

    }
    private void eat(String food){
        System.out.println("food = " + food);
    }
}
